Find the index with minimum score from a given array
Given an array A[] of size N(1 ? N ? 105), consisting of positive integers, where the score of an index i in range [0, N – 1] is defined as:...
read more
Find n-th element from Stern’s Diatomic Series
Given an integer n. we have to find the nth term of Stern’s Diatomic Series.Stern’s diatomic series is the sequence which generates the following integer sequence 0, 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4, ……. It arises in the Calkin-Wilf tree. It is sometimes also known as the fusc function.In mathematical terms, the sequence P(n) of Stern’s diatomic series is defined by the recurrence relation....
read more
Maximum profit by buying and selling a stock at most twice | Set 2
Given an array prices[] which denotes the prices of the stocks on different days, the task is to find the maximum profit possible after buying and selling the stocks on different days using transaction where at most two transactions are allowed.Note: You cannot engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again)....
read more
Count of submatrix with sum X in a given Matrix
Given a matrix of size N x M and an integer X, the task is to find the number of sub-squares in the matrix with sum of elements equal to X.Examples:...
read more
Iterative approach to print all combinations of an Array
Given an array arr[] of size N, the task is to generate and print all possible combinations of R elements in array. Examples:...
read more
Maximum subset sum such that no two elements in set have same digit in them
Given an array of N elements. Find the subset of elements which has maximum sum such that no two elements in the subset has common digit present in them.Examples:...
read more
Find minimum steps required to reach the end of a matrix | Set – 1
Given a 2D matrix consisting of positive integers, the task is to find the minimum number of steps required to reach the end(leftmost-bottom cell) of the matrix. If we are at cell (i, j) we can go to cells (i, j+arr[i][j]) or (i+arr[i][j], j). We can not go out of bounds. If no path exists, print -1....
read more
Number of ways of scoring R runs in B balls with at most W wickets
Given three integers R, B, and W which denote the number of runs, balls, and wickets. One can score 0, 1, 2, 3, 4, 6, or a wicket in a single ball in a cricket match. The task is to count the number of ways in which a team can score exactly R runs in exactly B balls with at-most W wickets. Since the number of ways will be large, print the answer modulo 1000000007. Examples:...
read more
Maximum sum path in a matrix from top to bottom
Consider a n*n matrix. Suppose each cell in the matrix has a value assigned. We can go from each cell in row i to a diagonally higher cell in row i+1 only [i.e from cell(i, j) to cell(i+1, j-1) and cell(i+1, j+1) only]. Find the path from the top row to the bottom row following the aforementioned condition such that the maximum sum is obtained....
read more
Count number of ways to jump to reach end
Given an array of numbers where each element represents the max number of jumps that can be made forward from that element. For each array element, count the number of ways jumps can be made from that element to reach the end of the array. If an element is 0, then a move cannot be made through that element. The element that cannot reach the end should have a count “-1“....
read more
Count of AP (Arithmetic Progression) Subsequences in an array
Given an array of n positive integers. The task is to count the number of Arithmetic Progression subsequence in the array. Note: Empty sequence or single element sequence is Arithmetic Progression. 1 <= arr[i] <= 1000000.Examples:...
read more
Count of subarrays whose maximum element is greater than k
Given an array of n elements and an integer k. The task is to find the count of the subarray which has a maximum element greater than K....
read more